<template>
	<view class="mypharmacy-wrap">
		<view class="myInquiry-tab">
			<u-tabs v-model="activeTab" :scroll="false" :tabs="['常用药品', '常用处方', '历史处方']" @change="tabChange"></u-tabs>
		</view>
		<scroll-view show-scrollbar="false" :class="[activeTab != 2 ? 'mypharmacy-scroller' : 'mypharmacy-scroller2']" scroll-y @scrolltolower="scrollLoading()">
			<view class="mypharmacy-label" v-if="activeTab == 0">
				<u-title :value="'常用药品列表（' + dataTotal + '）'" v-if="dataTotal > 0"></u-title>
				<view class="mypharmacy-list">
					<u-show-goods v-for="(item, index) in dataList" :key="index" :data="item.commodity" btnText="删除"
						type="1" @onClickBtn="deleteCommonUseCommodity(item.id)"></u-show-goods>
				</view>
				<u-bottom-button value="新增常用药品" @click="gotoRouter('/pages-doctor/drugList/drugList?category=45')">
				</u-bottom-button>
				<view style="height: calc(100vh - 530rpx);" v-if="dataTotalPage == 0">
					<u-empty-state value="暂无相关常用药品"></u-empty-state>
				</view>
			</view>

			<view class="mypharmacy-label" v-if="activeTab == 1">
				<u-title :value="'常用处方列表（' + dataTotal + '）'" v-if="dataTotal > 0"></u-title>
				<view class="mypharmacy-list">
					<u-presc-item v-for="(item, index) in dataList" :key="index" :data="item"
						@click="gotoRouter('/pages-doctor/editPresc/editPresc?id=' + item.id)" btnText="使用处方" type="0"
						:selectModel="selectModel" @clickOne="removeDrRecipe(item.id)" @clickTwo="useRecipeInfo(item)">
					</u-presc-item>
				</view>
				<view style="height: calc(100vh - 530rpx);" v-if="dataTotalPage == 0">
					<u-empty-state value="暂无相关常用处方"></u-empty-state>
				</view>
				<u-bottom-button type="2" value1="新建中药处方" value2="新建西药处方"
					@clickOne="gotoRouter('/pages-doctor/editPresc/editPresc?recipeType=1&diseasesName=' + diseasesName + '&diseasesCode=' + diseasesCode)"
					@clickTwo="gotoRouter('/pages-doctor/editPresc/editPresc?recipeType=0&diseasesName=' + diseasesName + '&diseasesCode=' + diseasesCode)"></u-bottom-button>
			</view>

			<view class="mypharmacy-label" v-if="activeTab == 2">
				<u-title :value="'历史处方列表（' + dataTotal + '）'" v-if="dataTotal > 0"></u-title>
				<view class="mypharmacy-list">
					<u-presc-item v-for="(item, index) in dataList" :key="index" :data="item" btnText="使用处方" type="0"
						@click="gotoRouter('/pages-doctor/prescInfo/prescInfo?id=' + item.id + '&role=1')"
						:selectModel="selectModel" @clickOne="removeHistoryRecipe(item.id)"
						@clickTwo="useRecipeInfo(item)"></u-presc-item>
				</view>
				<view style="height: calc(100vh - 530rpx);" v-if="dataTotalPage == 0">
					<u-empty-state value="暂无相关历史处方"></u-empty-state>
				</view>
			</view>

			<uni-load-more v-if="dataTotalPage > 1" :status="dataLoading"></uni-load-more>
		</scroll-view>
	</view>
</template>

<script>
import uTab from '@/components/u-tabs/u-tabs.vue';
import uTitle from '@/components/u-title/u-title.vue';
import uBottomButton from '@/components/u-bottom-button/u-bottom-button.vue';
import uShowGoods from '@/components/u-show-goods/u-show-goods.vue';
import uPrescItem from '@/components/u-presc-item/u-presc-item.vue';
import uniLoadMore from '@/components/uni/uni-load-more/uni-load-more.vue';
import uEmptyState from '@/components/u-empty-state/u-empty-state.vue';
import { mapState, mapMutations } from 'vuex';
export default {
	components: {
		uBottomButton,
		uTitle,
		uTab,
		uShowGoods,
		uPrescItem,
		uniLoadMore,
		uEmptyState
	},
	computed: {
		...mapState({
			recipeInfo: state => state.recipeInfo
		})
	},
	data() {
		return {
			activeTab: 0,
			pageNo: 1,
			pageSize: 20,
			dataTotal: 0, //数据总数
			dataTotalPage: 1, //数据总分页
			dataLoading: 'loading', //数据加载
			dataList: [],
			selectModel: false, //选择模式
			
			diseasesName: '', // 诊断结果
			diseasesCode: '', // 诊断结果
			
			from: '', // 来源哪一个页面，chatRoom=聊天室，casesEdit=写病历
		};
	},
	onLoad(option) {
		this.activeTab = option.activeTab ? parseInt(option.activeTab) : 0;
		this.selectModel = option.selectModel == 1 ? true : false;
		this.diseasesName = option.diseasesName;
		this.diseasesCode = option.diseasesCode;
		this.from = option.from
	},
	onShow() {
		this.initData();
	},
	methods: {
		...mapMutations({
			setRecipeInfo: 'setRecipeInfo'
		}),
		tabChange(e) {
			this.activeTab = e;
			this.initData();
		},
		initData() {
			this.pageNo = 1;
			this.dataList = [];
			this.dataTotalPage = 1;
			this.dataTotal = 0;
			this.scrollLoading();
		},
		//常用处方列表
		commonRecipe() {
			this.$api
				.commonRecipe({
					params: {
						pageNo: this.pageNo,
						pageSize: this.pageSize
					}
				})
				.then(res => {
					if (res.code == 200) {
						this.dataTotalPage = res.result.pages;
						this.dataTotal = res.result.total;
						if (this.pageNo == 1) {
							this.dataList = res.result.records;
						} else {
							this.dataList = this.dataList.concat(res.result.records);
						}

						if (res.result.current >= res.result.pages) {
							this.dataLoading = 'noMore';
						} else {
							this.dataLoading = 'loading';
						}
						this.pageNo++;
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				});
		},
		//常用药品列表
		queryCommonUseCommodityList() {
			this.$api
				.queryCommonUseCommodityList({
					params: {
						pageNo: this.pageNo,
						pageSize: this.pageSize
					}
				})
				.then(res => {
					if (res.code == 200) {
						this.dataTotalPage = res.result.pages;
						this.dataTotal = res.result.total;
						if (this.pageNo == 1) {
							this.dataList = res.result.records;
						} else {
							this.dataList = this.dataList.concat(res.result.records);
						}

						if (res.result.current >= res.result.pages) {
							this.dataLoading = 'noMore';
						} else {
							this.dataLoading = 'loading';
						}
						this.pageNo++;
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				});
		},
		//历史处方列表
		historyRecipePage() {
			this.$api
				.historyRecipePage({
					params: {
						pageNo: this.pageNo,
						pageSize: this.pageSize
					}
				})
				.then(res => {
					if (res.code == 200) {
						this.dataTotalPage = res.result.pages;
						this.dataTotal = res.result.total;
						if (this.pageNo == 1) {
							this.dataList = res.result.records;
						} else {
							this.dataList = this.dataList.concat(res.result.records);
						}

						if (res.result.current >= res.result.pages) {
							this.dataLoading = 'noMore';
						} else {
							this.dataLoading = 'loading';
						}
						this.pageNo++;
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				});
		},
		//删除常用药品
		deleteCommonUseCommodity(id) {
			uni.showModal({
				title: '确认要删除该常用药品吗？',
				success: res => {
					if (res.confirm) {
						this.$api
							.deleteCommonUseCommodity({
								params: {
									id: id
								}
							})
							.then(res => {
								if (res.code == 200) {
									uni.showToast({
										title: '删除成功',
										icon: 'none'
									});
									this.initData();
								} else {
									uni.showToast({
										title: res.message,
										icon: 'none'
									});
								}
							});
					}
				}
			});
		},
		//删除常用处方
		removeDrRecipe(id) {
			uni.showModal({
				title: '确认要删除该常用处方吗？',
				success: res => {
					if (res.confirm) {
						this.$api
							.removeDrRecipe({
								params: {
									id: id
								}
							})
							.then(res => {
								if (res.code == 200) {
									uni.showToast({
										title: '删除成功',
										icon: 'none'
									});
									this.initData();
								} else {
									uni.showToast({
										title: res.message,
										icon: 'none'
									});
								}
							});
					}
				}
			});
		},
		//删除历史处方
		removeHistoryRecipe(id) {
			uni.showModal({
				title: '确认要删除该历史处方吗？',
				success: res => {
					if (res.confirm) {
						this.$api
							.removeHistoryRecipe({
								params: {
									id: id
								}
							})
							.then(res => {
								if (res.code == 200) {
									uni.showToast({
										title: '删除成功',
										icon: 'none'
									});
									this.initData();
								} else {
									uni.showToast({
										title: res.message,
										icon: 'none'
									});
								}
							});
					}
				}
			});
		},
		//添加常用处方
		addDrRecipe(recipeForm) {
			this.$api
				.addDrRecipe({
					data: recipeForm
				})
				.then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '添加成功',
							icon: 'none'
						});
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				});
		},
		//选择模式使用处方
		useRecipeInfo(item) {
			let that = this
			uni.showModal({
				title: '确认使用该处方吗？',
				success: res => {
					if (res.confirm) {
						that.setRecipeInfo(item);
						if (that.from == 'chatRoom') {
							uni.navigateBack({
								delta: 1
							});
						}
						if (that.from == 'casesEdit') {
							uni.navigateBack({
								delta: 2
							});
						}
					}
				}
			});
		},
		//滚动加载
		scrollLoading() {
			if (this.pageNo <= this.dataTotalPage) {
				switch (this.activeTab) {
					case 0:
						this.queryCommonUseCommodityList();
						break;
					case 1:
						this.commonRecipe();
						break;
					case 2:
						this.historyRecipePage();
						break;
					default:
						break;
				}
			}
		}
	}
};
</script>

<style lang="less">
@import './mypharmacy.less';
</style>
